package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {

        //设置用户id,判断当前是那个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            //如果菜品id不为空，添加到购物车的就是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到购物车去的就是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(lqw);

        //如果在购物车上就加1，如果没有就添入购入车，默认数量为一

        if (shoppingCart1 != null) {
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number + 1);
            shoppingCartMapper.updateById(shoppingCart1);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
            shoppingCart1 = shoppingCart;
        }


        return shoppingCart1;
    }

    @Override
    public List<ShoppingCart> list() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(lqw);

        return shoppingCarts;
    }

    @Override
    public void clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartMapper.delete(lqw);
    }

    @Override
    public ShoppingCart sub(ShoppingCart shoppingCart) {


        //获取条件构造器
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //设置是那个用户的购物车
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //根据菜品id或着套餐id查询购物车里菜品的数量
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(lqw);
        if (shoppingCart1 == null) {
            shoppingCart1.setNumber(0);
            //return shoppingCart1;

        }
        if (shoppingCart1.getNumber() > 1) {
            shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
            shoppingCartMapper.update(shoppingCart1, lqw);

        } else if (shoppingCart1.getNumber() == 1) {
            int rows = shoppingCartMapper.delete(lqw);
            shoppingCart1.setNumber(0);

        }else{
            throw new CustomException("购物车里没有东西怎么删除");
        }
        System.out.println(shoppingCart1.getNumber());
        return shoppingCart1;
    }


}
